<?php
namespace app\api\controller;
use app\common\BaseController;
use app\common\Net;
use app\api\model\Policy as PolicyModel;
use think\Db;
class ObtainPolicyData extends BaseController{
    public function __construct(PolicyModel $policyModel){
        parent::__construct(); 
        $this->model = $policyModel;
    }
    
    public function index(){
        $params = $this->request->param();
        $data = $this->model->allList($params);
        return $data;
    }

    public function create(){            
        try{
            $params = $this->request->param();  
            $settleNo=$params['settleNo'];      
            $jsonArray=[
                "request"=>[
                    "loginName"=> "LETU",
                    "password"=> "Y/veTqvxj7UQ9IDCBBlqdA==",
                    "settleNo"=> $params['settleNo']
                ]
            ];
            $json = json_encode($jsonArray,JSON_UNESCAPED_UNICODE);            
            $en_param = [
                'mode' => "1",
                'content' => $json,
                'key'=>"auvY+v+HjNMmlvrR+k6CMD=="
            ]; 
            $en_res = Net::http("http://cw.tsletu.com:9999/letu/api",$en_param,'POST');            
            $res = Net::http("https://sxthd.cpic.com.cn/sxthd/cpic/obtainPolicyData ",$en_res,'POST');
            $de_param = [
                'mode' => "0",
                'content' => $res,
                'key'=>"auvY+v+HjNMmlvrR+k6CMD=="
            ]; 
            $de_res = Net::http("http://cw.tsletu.com:9999/letu/api",$de_param,'POST');
            $data_res = json_decode($de_res,true);
            $datas = $data_res['response'];
            $dir = ROOT_PATH . 'public' . DS . 'uploads'.DS."temp";
            if (!file_exists($dir)){
                mkdir ($dir,0777,true);               
            }
            $file = fopen(ROOT_PATH . 'public' . DS . 'uploads'.DS."temp".DS."d.zip", "w");
            fwrite($file,base64_decode($datas['resultFile']));
            fclose($file);
            
            $zip = new \ZipArchive;       
            $zip->open($dir.DS."d.zip");
            $zip->extractTo($dir.DS."d");     
            $zip->close();
            
            $size = filesize($dir.DS."d".DS.$params['settleNo'].".txt");
         
            if($size>0){
                $file = fopen($dir.DS."d".DS.$params['settleNo'].".txt","r");
                $info = fread($file,filesize($dir.DS."d".DS.$params['settleNo'].".txt"));
            
                fclose($file);
                $info=iconv('gbk', 'utf-8', $info);

                $arr = explode("|",$info);
                $count =count($arr);           
                $num = intval($count / 29);
                Db::startTrans();
                try{
                    for($i=0;$i<$num;$i++){
                        $params = [
                            "policy_no"=>$arr[$i*29+0],
                            "insure_status"=>$arr[$i*29+1],
                            "insurance_date"=>$arr[$i*29+2],
                            "begdate"=>$arr[$i*29+3],
                            "enddate"=>$arr[$i*29+4],
                            "insure_code"=>$arr[$i*29+5],
                            "product_name"=>$arr[$i*29+6],
                            "premium"=>$arr[$i*29+7],
                            "coverage"=>$arr[$i*29+8],
                            "policy_holder"=>$arr[$i*29+9],
                            "policy_holder_type"=>$arr[$i*29+10],
                            "policy_holder_idcard"=>$arr[$i*29+11],
                            "policy_holder_address"=>$arr[$i*29+12],
                            "insure_name"=>$arr[$i*29+13],
                            "insure_type"=>$arr[$i*29+14],
                            "insure_idcard"=>$arr[$i*29+15],
                            "insure_address"=>$arr[$i*29+16],
                            "job_no"=>$arr[$i*29+17],
                            "policy_holder_phone"=>$arr[$i*29+18],  
                            "company_name" =>"中国太平洋人寿保险股份有限公司"
                        ];
                        $params['type'] = $this->getType($params['insure_code']);
                        if($params['insure_status']=="IF"){
                            $model = new PolicyModel();    
                            $model->allowField(true)->save($params);
                        }                    
                    }
                    Db::commit(); 
                }catch(Exception $e){
                    Db::rollback();
                    echo "false";
                }       
                     
            }
            Db::table("lt_query_apply")->where('settleNo', $settleNo)->update(['is_sync' => 1]);    
            var_dump($datas);  
        }catch(Exception $e){
            echo "false";
        }
        
    }

    public function getType($value){
        //1个单，2团单
        $info = Db::table("lt_insure")->field('id')->where("insure_code",$value)->find();
        if(in_array($info['id'],[25,26,27,28,29,30,31,32,33,34,35,36,37])){
            return 2;
        }else{
            return 1;
        }
    }
}
